﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;
using SIPO.Library.DomainObject.Administration.Catalog;


namespace SIPO.Library.DataAccess.Transaction
{
    public partial class recInvoiceDetailRepo : ISearchable<recInvoiceDetail>
    {
      
        public object SelectScalar(SQL.Function.Aggregate function, string column, string paramaters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM transactions.recInvoiceDetail", column, paramaters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM transactions.recInvoiceDetail", column, paramaters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM transactions.recInvoiceDetail", column, paramaters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM transactions.recInvoiceDetail", column, paramaters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM transactions.recInvoiceDetail", column, paramaters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM transactions.recInvoiceDetail", column, paramaters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recInvoiceDetail> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string paremeters)
        {
            List<recInvoiceDetail> tt = new List<recInvoiceDetail>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat(@"      recInvoiceDetail.InvoiceDetailID
                                                , recInvoiceDetail.InvoiceNo
                                                , recInvoiceDetail.Value
                                                , recInvoiceDetail.TransTypeID
                                                , recInvoiceDetail.TariffID
                                                , recInvoiceDetail.BillingNo
                                                , recInvoiceDetail.DueDate
                                                , recInvoiceDetail.ForPayment
                                                , recInvoiceDetail.CreatedDate
                                                , recInvoiceDetail.CreatedBy
                                                , recInvoiceDetail.UpdatedDate
                                                , recInvoiceDetail.UpdatedBy
                                                ,recTransactionType.TransTypeName");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      transactions.recInvoiceDetail LEFT JOIN master.recTransactionType ON recTransactionType.TransTypeID = recInvoiceDetail.TransTypeID ");
                    sbQuery.AppendFormat("      {0} ",paremeters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recInvoiceDetail t = new recInvoiceDetail(true);
                                t.InvoiceDetailID = dr[recInvoiceDetail.Fields.InvoiceDetailID] == DBNull.Value ? (short)0 : short.Parse(dr[recInvoiceDetail.Fields.InvoiceDetailID].ToString());
                                t.InvoiceNo = dr[recInvoiceDetail.Fields.InvoiceNo] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.InvoiceNo].ToString();
                                t.Value = dr[recInvoiceDetail.Fields.Value] == DBNull.Value ? 0 : decimal.Parse(dr[recInvoiceDetail.Fields.Value].ToString());
                                t.TransTypeID = dr[recInvoiceDetail.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recInvoiceDetail.Fields.TransTypeID].ToString());
                                t.TariffID = dr[recInvoiceDetail.Fields.TariffID] == DBNull.Value ? (int?)null : int.Parse(dr[recInvoiceDetail.Fields.TariffID].ToString());
                                t.BillingNo = dr[recInvoiceDetail.Fields.BillingNo] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.BillingNo].ToString();
                                t.DueDate = dr[recInvoiceDetail.Fields.DueDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recInvoiceDetail.Fields.DueDate].ToString());
                                t.ForPayment = dr[recInvoiceDetail.Fields.ForPayment] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.ForPayment].ToString();
                                t.CreatedDate = dr[recInvoiceDetail.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recInvoiceDetail.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recInvoiceDetail.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recInvoiceDetail.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recInvoiceDetail.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recInvoiceDetail.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.UpdatedBy].ToString();

                                t.recTransactionType.TransTypeName = dr[recTransactionType.Fields.TransTypeName] == DBNull.Value ? string.Empty : dr[recTransactionType.Fields.TransTypeName].ToString();

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recInvoiceDetail> GetListByInvoiceNo(string invoiceNo)
        {
            List<recInvoiceDetail> tt = new List<recInvoiceDetail>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT InvoiceDetailID, InvoiceNo, Value, TransTypeID, TariffID, BillingNo, DueDate, ForPayment, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM transactions.recInvoiceDetail WHERE InvoiceNo = @InvoiceNo";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@InvoiceNo", SqlDbType.VarChar).Value = invoiceNo;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recInvoiceDetail t = new recInvoiceDetail();
                                t.InvoiceDetailID = dr[recInvoiceDetail.Fields.InvoiceDetailID] == DBNull.Value ? 0 : int.Parse(dr[recInvoiceDetail.Fields.InvoiceDetailID].ToString());
                                t.InvoiceNo = dr[recInvoiceDetail.Fields.InvoiceNo] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.InvoiceNo].ToString();
                                t.Value = dr[recInvoiceDetail.Fields.Value] == DBNull.Value ? 0 : decimal.Parse(dr[recInvoiceDetail.Fields.Value].ToString());
                                t.TransTypeID = dr[recInvoiceDetail.Fields.TransTypeID] == DBNull.Value ? (short)0 : short.Parse(dr[recInvoiceDetail.Fields.TransTypeID].ToString());
                                t.TariffID = dr[recInvoiceDetail.Fields.TariffID] == DBNull.Value ? (int?)null : int.Parse(dr[recInvoiceDetail.Fields.TariffID].ToString());
                                t.BillingNo = dr[recInvoiceDetail.Fields.BillingNo] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.BillingNo].ToString();
                                t.DueDate = dr[recInvoiceDetail.Fields.DueDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recInvoiceDetail.Fields.DueDate].ToString());
                                t.ForPayment = dr[recInvoiceDetail.Fields.ForPayment] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.ForPayment].ToString();
                                t.CreatedDate = dr[recInvoiceDetail.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recInvoiceDetail.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recInvoiceDetail.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recInvoiceDetail.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recInvoiceDetail.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recInvoiceDetail.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recInvoiceDetail.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }		
    }
}
